﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DemoBrick
{
    /// <summary>
    /// Bar
    /// </summary>
    class Bar
    {
        public Rectangle Rectangle { get; set; }
        public const int Step = 20;
        public const int BarWidth = 200; public int _gameWidth;

        public Bar(int gameWidth)
        {
            _gameWidth = gameWidth;
            Rectangle = new Rectangle(450, 600, BarWidth, 50);
        }
        /// <summary>
        /// 绘制Bar
        /// </summary>
        public void Draw(Graphics g)
        {
            g.FillRectangle(new SolidBrush(Color.Blue), Rectangle);
        }
        /// <summary>
        /// 处理键盘
        /// </summary>
        /// <param name="key"></param>
        public void KeyDown(String key)
        {
            int newX = Rectangle.X;


            if (key == "A")
            {
                newX -= Step;
                //防止左出界
                if (newX < 0) newX = 0;
            }
           
            if (key == "D")

            {
                newX += Step;
                
                if (newX+BarWidth >_gameWidth) newX = _gameWidth-BarWidth ;
            }

            Rectangle = new Rectangle(newX, Rectangle.Y, Rectangle.Width, Rectangle.Height);
        }
    }
}

